﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using System.Windows.Controls;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YCAFS05CHGR : DocumentBase
    {
        #region-------------Properties-----------------------
        DataRow LastRow = null;
        DataSet SetMap = null;
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        YCAFS05CHGRData m_Data, m_DataX; 

        public SETINTEXDB SetProd
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetProd");
            }
        }
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }

        CollectionViewSource vwEKPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKPOS");
            }
        }

        CollectionViewSource vwFDECK
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwFDECK");
            }
        }
        DataRow dwFDECK
        {
            get
            {
                if (vwFDECK.View == null)
                    return null;
                if ((DataRowView)vwFDECK.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwFDECK.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwWDECK
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwWDECK");
            }
        }
        #region=====vwABPAUF=======
        CollectionViewSource vwABPAUF
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwABPAUF");
            }
        }
        DataRow dwABPAUF
        {
            get
            {
                if (vwABPAUF.View == null)
                    return null;
                if ((DataRowView)vwABPAUF.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABPAUF.View.CurrentItem).Row;
            }
        }
        #endregion
        DataRow dwWDECK
        {
            get
            {
                if (vwWDECK.View == null)
                    return null;
                if ((DataRowView)vwWDECK.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwWDECK.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwFSSTUELI
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwFSSTUELI");
            }
        }

        DataRow dwFSSTUELI
        {
            get
            {
                if (vwFSSTUELI.View == null)
                    return null;
                if ((DataRowView)vwFSSTUELI.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwFSSTUELI.View.CurrentItem).Row;
            }
        }
        BindingListCollectionView cvFDECK
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgFDECK.ItemsSource);
            }
        }
        BindingListCollectionView cvWDECK
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgWDECK.ItemsSource);
            }
        }
        string ART
        {
            get
            {
                return m_Data.Left(txtMAT.Text.ToString() + "                      ", 20);
            }
        }
        string NSART
        {
            get
            {
                return m_Data.Left(txtNART.Text.ToString() + "                      ", 20);
            }
        }
        BindingListCollectionView cvFS05
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgFSSTUELI.ItemsSource);
            }
        }
        string user = "";
        string FART
        {
            get
            {
                return m_Data.Left(txtFART.Text.ToString() + "                      ", 20);
            }
            set
            {
                txtFART.Text = value;
            }
        }

        #endregion
        public YCAFS05CHGR()
        {
            InitializeComponent();
            this.Name = "原料替换";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            dicParamater["blBuildCellMastrix"] = false;
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            m_Data = new YCAFS05CHGRData(OracleDB, proxy);
            user = (string)dicParamater["User"];
            txtMAT.Text = "BG4X053";
            txtNART.Text = "BG4X053";
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                vwFSSTUELI.View.CurrentChanged += vwFSSTUELI_CurrentChanged;
                ResetSetInterface();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }
        public void ResetSetInterface()
        {
            SetMap = new SETINTEXDB();
            DataSet setResult = new DataSet();
            string filePath = Path.Combine(PathHelper.ApplicationPath, "Report\\Interface\\FSCHGMAPINTEX.XML");
            setResult.ReadXml(filePath);
            SetMap.Merge(setResult);
            m_Data.SetCAD = SetMap;
            m_Data.SetExt = new DataSet();
        }
        private void btnRefreshAB_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (FART.Trim() == "")
                    return;
                cvFDECK.CustomFilter = null;
                cvWDECK.CustomFilter = null;
                DataRow dwData = SetProd.FSSTUELI.NewRow();
                dwData["FSSMANDANT"] = "00";
                dwData["FSSART"] = FART;
                dwData["FSSMART"] = ART;
                DataSet SResult = m_Data.GetSProdData(dwData);
                SetProd.ABPOS.Clear();
                SetProd.FDECK.Clear();
                SetProd.WDECK.Clear();
                SetProd.ICC_ABAUFM.Clear();
                SetProd.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        void vwFSSTUELI_CurrentChanged(object sender, EventArgs e)
        {
            
        }
        private void btnQuerySData_Click(object sender, EventArgs e)
        {
            try
            {
                cvFS05.CustomFilter = null;
                DataSet SResult = new DataSet();
                using (new LongOperation(this, "数据更新中"))
                {
                    SResult = m_Data.GetSData(ART);
                    SetProd.Clear();
                    SetProd.Merge(SResult);
                    DataRow dwData = SetProd.FSSTAMM.NewRow();
                    dwData["FSMANDANT"] = "00";
                    dwData["FSART"] = Helper.Left(txtNART.Text.ToString() + "                                  ", 20);
                    SResult = m_Data.GetSNData(dwData);
                    SetData.Clear();
                    SetData.Merge(SResult);
                    txtNARTB.Text = txtNART.Text;
                }
                if (vwFSSTUELI.View != null)
                {
                    vwFSSTUELI.View.MoveCurrentToFirst();
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnRefreshFS05_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwFSSTUELI == null)
                    return;
                m_Data.INTEXDBC.SaveFSSTUELI(dwFSSTUELI);
                FART = dwFSSTUELI["FSSART"].ToString();
                //cvFS05.CustomFilter = "FSSART= '" + FART + "' AND FSSMART = '" + ART + "'";
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnRefreshDECK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataSet SResult = null;
                if (dwFDECK == null
                    &&dwWDECK==null)
                {
                    MessageBox.Show("请选择生产单！");
                    return;
                }
                string filter = "FSSART= '" + FART + "' AND FSSMART = '" + ART + "' ";
                DataRow[] rows = SetProd.Tables["FSSTUELI"].Select(filter);
                if (rows.Length > 0)
                {
                    MessageBox.Show("请更新所有相关BOM！");
                    //return;
                }
                if (dwFDECK != null)
                {
                    filter = "FDAUFKRS= '" + dwFDECK["FDAUFKRS"].ToString() + "' AND FDAUFLFD='" + dwFDECK["FDAUFLFD"].ToString() + "' AND FDART='" + ART + "'";
                    rows = SetProd.Tables["FDECK"].Select(filter);
                    foreach (DataRow rx in rows)
                    {
                        m_Data.INTEXDBC.DeleteFDECK(rx, 1);
                    }
                    filter = "ABAVKAUFKRS= '" + dwFDECK["FDAUFKRS"].ToString() + "' AND ABAVKAUFLFD='" + dwFDECK["FDAUFLFD"].ToString() + "' AND ABAART='" + ART + "'";
                    rows = SetProd.Tables["ICC_ABAUFM"].Select(filter);
                    foreach (DataRow rx in rows)
                    {
                        m_Data.INTEXDBC.DeleteICC_ABAUFM("", "", "", "", rx, 4);
                    }
                }
                if (dwWDECK != null)
                {
                    filter = "WDAUFKRS= '" + dwWDECK["WDAUFKRS"].ToString() + "' AND WDAUFLFD='" + dwWDECK["WDAUFLFD"].ToString() + "' AND WDART='" + ART + "'";
                    rows = SetProd.Tables["WDECK"].Select(filter);
                    foreach (DataRow rx in rows)
                    {
                        m_Data.INTEXDBC.DeleteWDECK(rx, 1);
                    }
                    filter = "ABAVKAUFKRS= '" + dwWDECK["WDAUFKRS"].ToString() + "' AND ABAVKAUFLFD='" + dwWDECK["WDAUFLFD"].ToString() + "' AND ABAART='" + ART + "'";
                    rows = SetProd.Tables["ICC_ABAUFM"].Select(filter);
                    foreach (DataRow rx in rows)
                    {
                        m_Data.INTEXDBC.DeleteICC_ABAUFM("", "", "", "", rx, 4);
                    }
                }
                cvFS05.CustomFilter = null;
                txtMessage.Text = "";
                DataTable tbBOM = null;
                filter = "FSSART= '" + dwFSSTUELI["FSSART"].ToString() + "'";
                rows = SetProd.Tables["FSSTUELI"].Select(filter);
                tbBOM = rows.CopyToDataTable();
                tbBOM.TableName = "FSSTUELI_EXT";
                DataTable tbABPOS = SetProd.ABPOS;
                string art = "";
                string var = "";
                string color = "";
                string size = "";
                DataRow rm = tbBOM.NewRow();
                var qAB = from DataRow row in tbABPOS.AsEnumerable()
                          where row["ABPAUFKRS"].ToString() == dwFDECK["FDAUFKRS"].ToString()
                          && row["ABPAUFLFD"].ToString() == dwFDECK["FDAUFLFD"].ToString()
                          select row;
                foreach (DataRow dwABPOS_EXT in qAB)
                {
                    art = dwABPOS_EXT["ABPART"].ToString();
                    var = dwABPOS_EXT["ABPVAR"].ToString();
                    color = dwABPOS_EXT["ABPFARBE"].ToString();
                    size = dwABPOS_EXT["ABPGROE"].ToString();
                    foreach (DataRow dwBOM in tbBOM.Rows)
                    {
                        rm.ItemArray = dwBOM.ItemArray;
                        //FSSVAR	FSSFARBE	FSSGROE	FSSAUFM	FSSVERP
                        //FSSMATKZ FSSMART	FSSMVAR	FSSMFARBE FSSMGROE	 	FSSVERBR FSSVBME	FSSVBGRP	FSSVBFKT
                        //FSSVBKZ==G 用量和尺码相关
                        string FSSVAR = rm["FSSVAR"].ToString();
                        string FSSFARBE = rm["FSSFARBE"].ToString().Trim();
                        string FSSGROE = rm["FSSGROE"].ToString();
                        string FSSAUFM = rm["FSSAUFM"].ToString();
                        string FSSVERP = rm["FSSVERP"].ToString();
                        string FSSPOSTYP = rm["FSSPOSTYP"].ToString();
                        //  if (FSSVAR.Trim() != var)
                        //   continue;

                        //============ 
                        string FSSPOS = rm["FSSPOS"].ToString();
                        string FSSMATKZ = rm["FSSMATKZ"].ToString();
                        string FSSMART = rm["FSSMART"].ToString();
                        string FSSMVAR = rm["FSSMVAR"].ToString();
                        string FSSMFARBE = rm["FSSMFARBE"].ToString();
                        string FSSMGROE = rm["FSSMGROE"].ToString();
                        string FSSVERBR = rm["FSSVERBR"].ToString();
                        string FSSVBME = rm["FSSVBME"].ToString();
                        string FSSVBFKT = rm["FSSVBFKT"].ToString();
                        string FSSVERL = rm["FSSVERL"].ToString();
                        double px = 1;
                        /*if (FSSMATKZ == "T")
                            px = 0.01;*/
                        FSSVERBR = (Convert.ToDouble(FSSVERBR) * px * Convert.ToDouble(FSSVBFKT) * 0.000001 * (1 + Convert.ToDouble(FSSVERL) * 0.0001)).ToString();
                        rm["FSSVERBR"] = FSSVERBR;
                        //先处理颜色
                        EventDispatcher.Instance.RaiseStatus("处理原料需求" + FSSMART + FSSMVAR + FSSMFARBE, StatusEventType.RunningProgress);
                        DataRow rtp = null;
                        if (m_Data.SetExt.Tables.Contains("FSSTUELI_EXT"))
                        {
                            m_Data.SetExt.Tables["FSSTUELI_EXT"].Clear();
                        }
                        DataTable tbFS05EXT = tbBOM.Clone();
                        DataRow rFS05 = tbFS05EXT.NewRow();
                        rFS05.ItemArray = rm.ItemArray;
                        if (FSSFARBE.Trim() == "*")
                        {
                            rFS05["FSSMFARBE"] = color;
                        }
                        tbFS05EXT.Rows.Add(rFS05);
                        m_Data.SetExt.Merge(tbFS05EXT);

                        if (FSSFARBE.Trim() != "")
                        {
                            if (FSSFARBE.Trim() != "*")
                            {
                                if (FSSFARBE.Trim() != color.Trim())
                                    continue;
                            }
                        }
                        if (FSSGROE.Trim() != "")
                        {
                            if (FSSGROE.Trim() != "*")
                            {
                                if (FSSGROE.Trim() != size.Trim())
                                    continue;
                            }
                        }
                        switch (FSSMATKZ)
                        {
                            case "F"://面料 
                                SResult = m_Data.Convert2WDECK(dwABPOS_EXT);
                                foreach (DataRow rw in SResult.Tables[0].Rows)
                                {
                                    m_Data.INTEXDBC.SaveWDECK(rw);
                                }
                                break;
                            case "T":
                                SResult = m_Data.Convert2FDECKB(dwABPOS_EXT);
                                foreach (DataRow rw in SResult.Tables[0].Rows)
                                {
                                    m_Data.INTEXDBC.SaveFDECK(rw);
                                    txtMessage.Text = rw["FDAUFKRS"].ToString() + rw["FDAUFLFD"].ToString() + rw["FDAUFLFD"].ToString() + "已经插入新的原料需求!";
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("处理完毕", StatusEventType.StopProgress);
            }
        }
        private void btnReplaceSReq_Click(object sender, RoutedEventArgs e)
        {
            if (dwWDECK == null
                && dwFDECK == null)
            {
                MessageBox.Show("请选择一行进行替换!");
                return;
            }
            string filter = "";
            DataRow[] rows = null;
            if (dwFDECK != null)
            {   
                 filter = "ABAVKAUFKRS= '" + dwFDECK["FDAUFKRS"].ToString() 
                    + "' AND ABAVKAUFLFD='" + dwFDECK["FDAUFLFD"].ToString()   
                    + "' AND ABAVKAUFPOS='" + dwFDECK["FDAUFPOS"].ToString() + "' AND ABAART='" + ART + "' AND ABAVKUPOS='" +dwFDECK["FDSETPOS"].ToString().Substring(1,2) + "'";
                rows = SetProd.Tables["ICC_ABAUFM"].Select(filter);
                foreach (DataRow rx in rows)
                {
                    if (rx["ABAAUFTYP"].ToString().Trim() != "N")
                    {
                        MessageBox.Show("请到采购单管理处，先释放需求！");
                        return;
                    }
                    rx["ABAART"] = NSART;
                    m_Data.INTEXDBC.DeleteICC_ABAUFM("", "", "", "", rx, 4);
                }
                dwFDECK["FDART"] = dwFSSTUELI["FSSMART"];
                dwFDECK["FDFARBE"] = dwFSSTUELI["FSSMFARBE"];
                dwFDECK["FDGROE"] = dwFSSTUELI["FSSMGROE"];
                m_Data.INTEXDBC.UpdateFDECK(dwFDECK, "", 1);
            }
            if (dwWDECK != null)
            {
                filter = "ABAVKAUFKRS= '" + dwWDECK["WDAUFKRS"].ToString()
                    + "' AND ABAVKAUFLFD='" + dwWDECK["WDAUFLFD"].ToString()
                    + "' AND ABAVKAUFPOS='" + dwWDECK["WDAUFPOS"].ToString() + "' AND ABAART='" + ART + "' AND ABAVKUPOS='" + dwWDECK["WDUPOS"].ToString()+ "'";
                rows = SetProd.Tables["ICC_ABAUFM"].Select(filter);
                foreach (DataRow rx in rows)
                {
                    if (rx["ABAAUFTYP"].ToString().Trim() !="N")
                    {
                        MessageBox.Show("请到采购单管理处，先释放需求！");
                        return;
                    }
                    rx["ABAART"] = NSART;
                    rx["ABAFARBE"] =Helpers.Helper.Left( cbSFFARBE.Text.ToString()+"             ",10);
                    m_Data.INTEXDBC.DeleteICC_ABAUFM("", "", "", "", rx, 4);
                }
                dwWDECK["WDART"] = NSART;
                dwWDECK["WDFARBE"] = Helpers.Helper.Left(cbSFFARBE.Text.ToString() + "             ", 10);
                m_Data.INTEXDBC.UpdateWDECK(dwWDECK, "", 1);
            }
        }
        private void ABP_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dwABPAUF == null)
                return;
            //FilterWD();
            //FilterFD();
        }
        private void FilterWD()
        {
            string filter = "WDAUFKRS='" + dwABPAUF["ABPAUFKRS"].ToString() + "' AND WDAUFLFD ='" + dwABPAUF["ABPAUFLFD"].ToString() + "'";
            cvWDECK.CustomFilter = filter;
        }
        private void FilterFD()
        {
            string filter = "FDAUFKRS='" + dwABPAUF["ABPAUFKRS"].ToString() + "' AND FDAUFLFD='" + dwABPAUF["ABPAUFLFD"].ToString() + "'";
            cvFDECK.CustomFilter = filter;
        }
        private void dlgFSSTUELI_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (dwFSSTUELI == null)
                return;
            dwFSSTUELI["FSSMART"] = Helper.Left(txtNART.Text.ToString() + "                    ", 20);
            btnRefreshAB_Click(null, null);
            string filter = "ABPART='" + dwFSSTUELI["FSSART"].ToString() + "' "
                + " AND FDART='" + dwFSSTUELI["FSSMART"].ToString() + "'"
                + " AND FDFARBE='" + dwFSSTUELI["FSSMFARBE"].ToString() + "'"
                + " AND FDGROE='" + dwFSSTUELI["FSSMGROE"].ToString() + "'"
                ;
            cvFDECK.CustomFilter = filter;
            filter = "ABPART='" + dwFSSTUELI["FSSART"].ToString() + "' "
               + " AND WDART='" + dwFSSTUELI["FSSMART"].ToString() + "'"
               + " AND WDFARBE='" + dwFSSTUELI["FSSMFARBE"].ToString() + "'"
               ;
            cvWDECK.CustomFilter = filter;
        }
    }
}
